home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9346 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.3 KB  |  47 lines

  1. Path: news.PBI.net!usenet
  2. From: mich@pbinet.com
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Q: How to assign structures?
  5. Date: 9 Mar 1996 17:09:35 GMT
  6. Organization: Pacific Bell Internet Services
  7. Message-ID: <4hse0f$efq@SNFC21_SRVR_WWW.PBI.net>
  8. References: <4gsalu$dll@ccshst05.cs.uoguelph.ca>
  9. Reply-To: mich@pbinet.com
  10. NNTP-Posting-Host: ppp-5-36.rdcy01.pbinet.com
  11. X-Newsreader: IBM NewsReader/2 v1.03
  12.  
  13. In <4gsalu$dll@ccshst05.cs.uoguelph.ca>, thay@uoguelph.ca (Toby K Hay) writes:
  14. >I need to assign values to structures in one array from structures in 
  15. >another array - in essence I'm copying the whole structure from one array 
  16. >to the other.  Can I use simple assignment like this:
  17.  
  18. >struct StructName {
  19. >    int     IVal;
  20. >    float    FVal;}
  21.  
  22. >struct StructName Array1[5], Array2[5];
  23. >.. . .
  24. >for (i=0;i<5;i++) Array1[i] = Array2[i];
  25. >.. . .
  26. >And if not that, can I use memcpy() and sizeof() to do the assignment:
  27.  
  28. >for (i=0;i<5;i++) memcpy(&Array2[i],&Array1[i],sizeof(struct StructName));
  29. >Or do I have to assign each element of the structure?
  30.  
  31. It would be a lot easier to create a pointer to the struct and then make a copy
  32. of the pointer;
  33.  
  34. struct mystruct {..} ms, *ps;
  35.  
  36. main()
  37. {
  38.     void *ns;
  39.  
  40.     ps = &ms;
  41.     ns = ps;
  42. }
  43.  
  44. This is an overly simple example, you might want to malloc the new pointer or
  45. something, I dunno, but this a basic approach.
  46.  
  47.